import 'dart:io';

import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import '../utils/phone_info_utils.dart';
import '../utils/share_pre_utils.dart';

class DataHelper {
  ///基础请求参数（token，版本号等）post请求query入参
  ///
  ///
  /// 公用请求参数 带token 不带params
  static Map getBaseMap() {
    var formData = {
      "access_token": SharePreUtils.getToken(),
      "app_id": Platform.isAndroid
          ? "100004"
          : "100004", //终端 1001-小程序 1002-ios 1003-Android
      "app_version": PhoneInfoUtils.getAppVersion(),
      "timestamp": PhoneInfoUtils.getTimeStamp(),
    };
    formData["sign"] = getSign(formData); //签名

    return formData;
  }

  /// 基础请求参数 带params
  static Map getBaseMapParams(Map params) {
    var formData = {
      "access_token": SharePreUtils.getToken(),
      "app_id": Platform.isAndroid
          ? "100004"
          : "100004", //终端 1001-小程序 1002-ios 1003-Android
      "app_version": PhoneInfoUtils.getAppVersion(),
      "timestamp": PhoneInfoUtils.getTimeStamp(),
      "params": json.encode(params).toString(),
    };
    formData["sign"] = getSign(formData); //签名
    return formData;
  }

  ///基础请求参数（不带params、不带token）
  static Map getBaseMapParamsWithoutToken() {
    var formData = {
      "app_id": Platform.isAndroid
          ? "100004"
          : "100004", //终端 1001-小程序 1002-ios 1003-Android
      "app_version": PhoneInfoUtils.getAppVersion(),
      "timestamp": PhoneInfoUtils.getTimeStamp(),
    };
    formData['sign'] = getSign(formData);
    return formData;
  }

  ///基础请求参数（带params、不带token）
  static Map getBaseMapWithParamsWithoutToken(Map params) {
    var formData = {
      "app_id": Platform.isAndroid
          ? "100004"
          : "100004", //终端 1001-小程序 1002-ios 1003-Android
      "app_version": PhoneInfoUtils.getAppVersion(),
      "timestamp": PhoneInfoUtils.getTimeStamp(),
      "params": json.encode(params).toString(),
    };
    formData['sign'] = getSign(formData);
    return formData;
  }

  ///对sign进行 MD5或者Sha1 加密得到sign字段值
  static String getSign(Map parameter) {
    var key = "ZWywbT9IUgEIoOfOvUfXl7KtaB110nAX";

    if (Platform.isAndroid) key = "ZWywbT9IUgEIoOfOvUfXl7KtaB110nAX";

    ///存储所有key
    List<String> allKeys = [];
    parameter.forEach((key, value) {
      allKeys.add(key + value);
    });

    ///key排序
    allKeys.sort((obj1, obj2) {
      return obj1.compareTo(obj2);
    });

    ///数组转String
    String pairString = allKeys.join("");

    ///拼接ABC是你的秘钥
    String sign = key + pairString + key;

    ///hash
    String signString = string2MD5(sign).toLowerCase();
    // String signString = stringSha1(sign).toLowerCase();
    print("sha1加密后------ $signString");
    return signString;
  }

  ///加密
  static string2MD5(String data) {
    var content = const Utf8Encoder().convert(data);

    //md5加密
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }

  static stringSha1(String data) {
    var content = const Utf8Encoder().convert(data);

    //sha1加密
    var digest = sha1.convert(content);
    return digest.toString();
  }
}
